﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.Core.Modelling;


namespace ITAP.ContourBuilder.Components.Constraints
{
    [Serializable]
    public class ConstraintStickCommentLineToSource : Constraint
    {
        public override bool Check(IContourComponent component)
        {
            return false;
        }

        public override void Fix(IContourComponent component)
        {
            if (!enabled)
                return;

            if (!(component is ContourCommentLine))
                return;

            ContourCommentLine line = component as ContourCommentLine;

            foreach (IContourComponent connectedComponent in component.ConnectedComponents)
            {
                if (connectedComponent is ICommentSource)
                {
                    if(line.StartConnectedComponent == connectedComponent)
                    {
                        ModelPoint point = (connectedComponent as ICommentSource).GetNearestCommentPoint(line.StartPoint);
                        line.MoveStartPoint(point);
                    }
                    else if(line.EndConnectedComponent == connectedComponent)
                    {
                        ModelPoint point = (connectedComponent as ICommentSource).GetNearestCommentPoint(line.EndPoint);
                        line.MoveEndPoint(point);
                    }
                    else
                        throw new Exception();                    
                }
            }
        }
    }
}
